<template>

  <div class="app-container">


    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        
        <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
      
        <label class="el-form-item-label">题目类型</label>
        <el-select v-model="query.subType">
        <el-option
          v-for="item in options1"
          :key="item.value1"
          :label="item.label"
          :value="item.value1"
           class="filter-item" @keyup.enter.native="crud.toQuery">
           </el-option>
          </el-select>

          <label class="el-form-item-label">隶属科目</label>
        <el-select v-model="query.topicType">
        <el-option
          v-for="item in options2"
          :key="item.value2"
          :label="item.label"
          :value="item.value2"
           class="filter-item" @keyup.enter.native="crud.toQuery">
           </el-option>
          </el-select>
          

        <rrOperation :crud="crud" />

      </div> 

        
      </div>
      <!-- 如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <br>
      <div class="you">
      <rrOperation :crud="crud"/>
      </div>

      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="1000px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="题目名称" prop="id">
            <el-input v-model="form.subName" style="width: 800px;" 
              type="textarea"
              :autosize="{minRows:5,maxRows:4}"
              placeholder="请输入内容"/>
              <template>
               <br style="clear: both;">      
             <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
 
 <br>
     <el-row :gutter="150" >
  <el-col :span="50"><el-form-item label="选择题型" prop="id">
    <template>
        <span>题目类型</span>
          <el-select v-model="form.subType" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          </template>
          </el-form-item>  </el-col>


  <el-col :span="50"><el-form-item label="隶属科目" prop="id">
          <el-select
            v-model="form.topicType"
            collapse-tags
            placeholder="请选择">
            <el-option
              v-for="item in options2"
              :key="item.value2"
              :label="item.label"
              :value="item.label">
            </el-option>
          </el-select>
          </el-form-item>  </el-col>

</el-row>
      </div>
          </template>

         

            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      
      <el-table ref="table" v-loading="crud.loading" :data="crud.data"  border size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="50" />
        <el-table-column prop="id" label="编号"  width="50" />
        <el-table-column prop="subName" label="题目名称" />
        <el-table-column prop="subType" :formatter="Types" label="题目类型"/>
        <el-table-column prop="addPerson"  :formatter="showNewsData"  label="题目正确答案" />
           <el-table-column prop="topicType" :formatter="subjects"  label="隶属科目" />
        <el-table-column v-if="checkPer(['admin','baeSubject:edit','baeSubject:del'])" label="操作" width="150px" align="center">
          
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudBaeSubject from '@/api/baeSubject'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, subName: null, subType: null, addTime: null, addPerson: null, subFraction: null, subPicture: null, subUse: null, subCity: null, subExplainTime: null, successKeys: null, subNo: null, topicType: null }
export default {
  name: 'BaeSubject',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '添加题目', url: 'api/baeSubject', idField: 'id', sort: 'id,desc', crudMethod: { ...crudBaeSubject }})
  },
  data() {
    return {
       options1: [{
                value1: '1',
                label: '单选'
              }, {
                value1: '2',
                label: '多选'  
              }, {
                value1: '3',
                label: '判断'  
              }],
        options2: [{
                value2: '0',
                label: '文字类'
              }, {
                value2: '1',
                label: '图文类'
              }],        
                value1: "",
                value2:"",
      SubjectKeyMsg : [],
      permission: {
        add: ['admin', 'baeSubject:add'],
        edit: ['admin', 'baeSubject:edit'],
        del: ['admin', 'baeSubject:del']
      },
      rules: {
        id:[
          { required: true, message: '主键不能为空', trigger: 'blur', },
        ]
      },
            

      props: {
          label: 'name',
          children: 'zones',
          isLeaf: 'leaf'
        },
      };
    },

      mounted:function(){
    this.stateFormat()
 
  },
    methods: {
      loadNode(node, resolve) {
        if (node.level === 0) {
          return resolve([{ name: 'region' }]);
        }
        if (node.level > 1) return resolve([]);

        setTimeout(() => {
          const data = [{
            name: 'leaf',
            leaf: true
          }, {
            name: 'zone'
          }];

          resolve(data);
        }, 500);


  },
      
},
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
     showNewsData(row) { 
       console.log(this.SubjectKeyMsg[1].answer)
        return this.SubjectKeyMsg[row.id-1].answer

     
    },

    Types(row){
      if (row.subType === 1) {
            return '单选'
          } else if (row.subType === 2){
            return '多选'
          } else{
            return '判断'
          }

    },
    subjects(row){
        if (row.topicType === 0) {
              return '文字类'
            } else  {
              return '图文类'
            } 
    },
    async stateFormat() {
        var self = this;
      await this.$axios.get('http://81.71.147.62:8000/api/baeSubjectKey/showAllSubSubjectKey',{
    }).then(data =>{
        self.SubjectKeyMsg = data.data.SubjectKeyMsg


    });
  },
    
  }
}
</script>

<style scoped>
 .el-row {
    margin-bottom: 20px;
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }

  .you{
      margin-top: 8px;
      float: right;
      }

</style>
